package com.zhazha.stand.logic.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.zhazha.stand.entity.Contest;
import com.zhazha.stand.entity.Department;
import com.zhazha.stand.entity.Round;
import com.zhazha.stand.entity.User;
import com.zhazha.stand.entity.UserScore;
import com.zhazha.stand.logic.InformationLogic;
import com.zhazha.stand.mapper.ContestMapper;
import com.zhazha.stand.mapper.DepartmentMapper;
import com.zhazha.stand.mapper.RoundMapper;
import com.zhazha.stand.mapper.UserMapper;
import com.zhazha.stand.mapper.UserScoreMapper;
import com.zhazha.stand.model.DepartmentScoreModel;
import com.zhazha.stand.model.LevelScoreModel;
import com.zhazha.stand.model.UserScoreModel;
import com.zhazha.stand.util.MybatisUtil;

public class InformationLogicImpl implements InformationLogic {

	@Override
	public List<DepartmentScoreModel> getDepartmentScoreList(int contestId) {
		// TODO Auto-generated method stub
		SqlSession session = null;
		List<DepartmentScoreModel> result = null;
		try {
			session = MybatisUtil.getSession();
			
			//获取所有的department
			DepartmentMapper dmMapper = session.getMapper(DepartmentMapper.class);
			List<Department> dmList = dmMapper.getList();
			session.commit();
			if (null == dmList)
				return null;
			
			List<UserScoreModel> scoreList = this.getAllUserScoreList(contestId);
			if (null == scoreList)
				return null;
			
			int number = 0;
			int score = 0;
			String contestName =scoreList.get(1).getContestName();
			for (int i = 0 ; i < dmList.size(); i++)
			{
				Department tempDm = dmList.get(i);
				for (int j = 0; j < scoreList.size(); j++)
				{
					UserScoreModel tempScore = scoreList.get(j);
					if (tempDm.getName() == tempScore.getDepartmentName())
					{
						number++;
						score += tempScore.getScore();
					}
				}
				if (null == result)
					result = new ArrayList<DepartmentScoreModel>();
				result.add(new DepartmentScoreModel(tempDm.getName(), score, number, contestName));
			}
			
			MybatisUtil.clossSession();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

	@Override
	public List<UserScoreModel> getAllUserScoreList(int contestId) {
		// TODO Auto-generated method stub
		SqlSession session = null;
		List<UserScoreModel> result = null;
		try {
			session = MybatisUtil.getSession();
			
			//获取Contest
			ContestMapper contestMapper = session.getMapper(ContestMapper.class);
			Contest contest = contestMapper.getById(contestId);
			session.commit();
			if (null == contest)
				return null;
			
			//获取所有的department
			DepartmentMapper dmMapper = session.getMapper(DepartmentMapper.class);
			List<Department> dmList = dmMapper.getList();
			session.commit();
			if (null == dmList)
				return null;
			
			//获取所有User
			UserMapper userMapper = session.getMapper(UserMapper.class);
			List<User> userList = userMapper.getList();
			session.commit();
			if (null == userList)
				return null;
			
			//获取所有的userscore
			UserScoreMapper userScoreMapper = session.getMapper(UserScoreMapper.class);
			List<UserScore> scoreList = userScoreMapper.getByContestId(contestId);
			session.commit();
			if (null == scoreList)
				return null;
			
			//开始处理数据
			boolean flag = false;
			for (int i = 0; i < userList.size(); i++)
			{
				User tempUser = userList.get(i);
				UserScoreModel model = null;
				for (int j = 0; j < scoreList.size(); j++)
				{
					UserScore tempScore = scoreList.get(j);
					if (tempUser.getId() == tempScore.getUserId())
					{
						if (!flag)
						{
							model = new UserScoreModel();
							model.setContestName(contest.getName());
							model.setName(tempUser.getName());
							model.setGrd(tempUser.getGrd());
							model.setScore(tempScore.getScore());
							for (int k = 0; k < dmList.size(); k++)
								if (dmList.get(k).getId() == tempUser.getDepartmentId())
								{
									model.setDepartmentName(dmList.get(k).getName());
									break;
								}
							flag = true;
						}
						else
						{
							model.setScore(model.getScore() + tempScore.getScore());
						}
					}
				}
				if (null != model)
				{
					model.setLevel(this.levelRule(model.getScore()));
					if (null == result)
						result = new ArrayList<UserScoreModel>();
					result.add(model);
				}
				flag = false;
			}
			
			MybatisUtil.clossSession();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}
	
	private int levelRule(int score) {
		
		if (score == 150)
			return 5;
		else if ((score < 150) && (score >= 120))
			return 4;
		else if ((score < 120) && (score >= 90))
			return 3;
		else if ((score < 90) && (score >= 60))
			return 2;
		else if ((score < 60) && (score >= 30))
			return 1;
		else if (score < 30)
			return 0;
		else
			return 0;
	}

	@Override
	public float getGrdAverageScore(int contestId) {
		// TODO Auto-generated method stub
		float score = 0;
		int number = 0;
		try {
			List<UserScoreModel> scoreList = this.getAllUserScoreList(contestId);
			if (null == scoreList)
				return 0;
			for (int i = 0; i < scoreList.size(); i++)
			{
				if (1 == scoreList.get(i).getGrd())
				{
					score += scoreList.get(i).getScore();
					number++;
				}	
			}
		} finally {
			
		}
		score = score / number;
		return score;
	}

	@Override
	public float getNotGrdAverageScore(int contestId) {
		// TODO Auto-generated method stub
		float score = 0;
		int number = 0;
		try {
			List<UserScoreModel> scoreList = this.getAllUserScoreList(contestId);
			if (null == scoreList)
				return 0;
			for (int i = 0; i < scoreList.size(); i++)
			{
				if (0 == scoreList.get(i).getGrd())
				{
					score += scoreList.get(i).getScore();
					number++;
				}	
			}
		} finally {
			
		}
		score = score / number;
		return score;
	}

	@Override
	public List<LevelScoreModel> getLevelAverageScore(int contestId) {
		// TODO Auto-generated method stub
		SqlSession session = null;
		List<LevelScoreModel> result = null;
		try {
			session = MybatisUtil.getSession();
			RoundMapper mapper = session.getMapper(RoundMapper.class);
			List<Round> roundList = mapper.getByContestId(contestId);
			session.commit();
			
			//获取所有的userscore
			UserScoreMapper userScoreMapper = session.getMapper(UserScoreMapper.class);
			List<UserScore> scoreList = userScoreMapper.getByContestId(contestId);
			session.commit();
			if (null == scoreList)
				return null;
			
			for (int i = 0; i < roundList.size(); i++)
			{
				LevelScoreModel model = new LevelScoreModel();
				model.setScore(0);
				model.setNumber(0);
				model.setLevel(roundList.get(i).getLevel());
				for (int j = 0; j < scoreList.size(); j++)
				{
					if (roundList.get(i).getLevel() == scoreList.get(j).getLevel())
					{
						model.setScore(model.getScore() + scoreList.get(j).getScore());
						model.setNumber(model.getNumber() + 1);
					}
				}
				if (result == null)
					result = new ArrayList<LevelScoreModel>();
				result.add(model);
			}
				
			MybatisUtil.clossSession();
		} finally {
			
		}
		return result;
	}


}
